/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package modelos;

import com.jme.animation.*;
import com.jme.scene.*;
import com.jme.util.resource.*;
import com.jmex.model.collada.ColladaImporter;
import java.io.*;
import java.net.*;
import java.util.ArrayList;
import java.util.logging.*;
import jmetest.renderer.loader.TestColladaLoading;

/**
 *
 * @author J
 */
public class ColladaModelLoader {

  private static final Logger logger = Logger.getLogger(TestColladaLoading.class.getName());

  public static Node loadModel(String modelName) {
    try {
      ResourceLocatorTool.addResourceLocator(
              ResourceLocatorTool.TYPE_TEXTURE,
              new SimpleResourceLocator(ColladaModelLoader.class.getClassLoader().getResource(
              "modelos/")));
    } catch (URISyntaxException e1) {
      logger.warning("Unable to add texture directory to RLT: " + e1.toString());
    }
    AnimationController ac;
    Node modelNode = new Node();
    URL modelUrl = ColladaModelLoader.class.getClassLoader().getResource("modelos/"+modelName);
    InputStream modelInput = ColladaModelLoader.class.getClassLoader().getResourceAsStream("modelos/" + modelName);
    //this stream points to the animation file. Note: You don't necessarily
    //have to split animations out into seperate files, this just helps.
    InputStream animationInput = ColladaModelLoader.class.getClassLoader().getResourceAsStream("modelos/" + modelName);
    if (modelInput == null) {
      logger.info("File not found!");
      System.exit(0);
    }
    //tell the importer to load the mob boss
    ColladaImporter.load(modelInput, "model");
    //we can then retrieve the skin from the importer as well as the skeleton
    SkinNode sn = ColladaImporter.getSkinNode(ColladaImporter.getSkinNodeNames().get(0));
    Bone skel = ColladaImporter.getSkeleton(ColladaImporter.getSkeletonNames().get(0));
    //clean up the importer as we are about to use it again.
    ColladaImporter.cleanUp();

    //load the animation file.
    ColladaImporter.load(animationInput, "anim");
    //this file might contain multiple animations, (in our case it's one)
    ArrayList<String> animations = ColladaImporter.getControllerNames();
    if (animations != null) {
      logger.info("Number of animations: " + animations.size());
      for (int i = 0; i < animations.size(); i++) {
        logger.info(animations.get(i));
      }
      //Obtain the animation from the file by name
      BoneAnimation anim1 = ColladaImporter.getAnimationController(animations.get(0));

      //set up a new animation controller with our BoneAnimation
      ac = new AnimationController();
      ac.addAnimation(anim1);
      ac.setRepeatType(Controller.RT_WRAP);
      ac.setActive(true);
      ac.setActiveAnimation(anim1);

      //assign the animation controller to our skeleton
      skel.addController(ac);
    }

    //attach the skeleton and the skin to the rootnode. Skeletons could possibly
    //be used to update multiple skins, so they are seperate objects.
    modelNode.attachChild(sn);
    modelNode.attachChild(skel);

    modelNode.updateGeometricState(0, true);
    //all done clean up.
    ColladaImporter.cleanUp();
    try {
      modelInput.close();
      animationInput.close();
    } catch (IOException ex) {
      Logger.getLogger(ColladaModelLoader.class.getName()).log(Level.SEVERE, null, ex);
    }
    return modelNode;
  }
}
